package com.example.yx.demo13;
import com.example.yx.demo13.A13.*;

import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

public class $Proxy0 implements Foo {


    static Method foo;
    static Method bar;
    static Method bar1;

    //
    static {
        try {
            foo = Foo.class.getMethod("foo");
            bar= Foo.class.getMethod("bar");
            bar1= Foo.class.getMethod("bar1");
        } catch (NoSuchMethodException e) {
            throw new NoSuchMethodError(e.getMessage());
        }
    }


    //拥有一个可以识别具体需要代理的方法的接口
    private InvocationHandler h;

    //该构造必须传入可以识别具体需要代理的方法的接口，以便能正确使用
    public $Proxy0(InvocationHandler h) {
        this.h = h;
    }

    @Override
    public void foo() throws NoSuchMethodException {
        try {
            //拿到具体方法的名字,考虑到接口最好只需被加载一次，选择将用于接收方法对象写成静态的成员变量，并在静态代码块中对他们进行加载

            //将该方法的信息传入
            h.invoke(this,foo,new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }

    @Override
    public void bar() throws NoSuchMethodException {
        try {
            h.invoke(this,bar,new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }

    @Override
    public int bar1() {
        try {
            Object result = h.invoke(this,bar1, new Object[0]);
            return (int) result;
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }

}
